import { reactive, ref } from "vue";
import type { FormRules } from "element-plus";

/** 密码正则（密码格式应为8-18位数字、字母、符号的任意两种组合） */
export const REGEXP_PWD =
  /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)]|[()])+$)(?!^.*[\u4E00-\u9FA5].*$)([^(0-9a-zA-Z)]|[()]|[a-z]|[A-Z]|[0-9]){8,18}$/;

/** 邮箱校验 */
export const EMAIL_REGEX = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

/** 中文校验 */
export const CHINESE_REGEX = /^[\u4e00-\u9fa5]{0,}$/;

/**是否合法IP地址*/
export const IP_REGEX =
  /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;

/**是否手机号码或者固话*/
export const PHONE_REGEX = /^((0\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/;

/**是否身份证号码*/
export const CRAD_REGEX = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;

/**整数填写*/
export const INT_REGEX = /^-?[1-9]\d*$/;

/**正整数填写*/
export const INTS_REGEX = /^[1-9]\d*$/;

/**小写字母*/
export const LETTER__REGEX = /^[a-z]+$/;

/**大写字母*/
export const LETTER_ING_REGEX = /^[A-Z]+$/;

/**大小写混合*/
export const LETTER_DX_REGEX = /^[A-Za-z]+$/;

/**多个8位数字格式(yyyyMMdd)并以逗号隔开*/
export const INTS_EIGHT_REGEX = /^\d{8}(\,\d{8})*$/;

/**数字加英文，不包含特殊字符*/
export const INTS_EN_REGEX = /^[a-zA-Z0-9]+$/;

/**前两位是数字后一位是英文*/
export const EN_INTS_REGEX = /^\d{2}[a-zA-Z]+$/;

/**时间格式校验 */
export const regex: RegExp = /\d{2}:\d{2}:\d{2}/;
/** 登录校验 */
const loginRules = reactive(<FormRules>{
  password: [
    {
      validator: (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请输入密码"));
        } else if (!REGEXP_PWD.test(value)) {
          callback(
            new Error("密码格式应为8-18位数字、字母、符号的任意两种组合")
          );
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ]
});

const checkUserName = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("姓名不可为空"));
  } else if (!CHINESE_REGEX.test(value)) {
    callback(new Error("请输入中文姓名"));
  } else {
    return callback();
  }
};
const checkchine = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("该位置不可为空"));
  } else if (!CHINESE_REGEX.test(value)) {
    callback(new Error("请输入中文字符"));
  } else {
    return callback();
  }
};

const checkPasword = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else if (!REGEXP_PWD.test(value)) {
    callback(new Error("密码格式应为8-18位数字、字母、符号的任意两种组合"));
  } else {
    callback();
  }
};


function hasValue(obj) {
  for (let key in obj) {
    if (obj.hasOwnProperty(key) && obj[key] !== undefined) {
      return true;
    }
  }
  return false;
}

export {
  loginRules,
  checkUserName,
  checkPasword,
  checkchine,
  hasValue
};
